package cn.kgc.controller;

import cn.kgc.bean.dto.UserDto;
import cn.kgc.bean.pojo.CCourt;
import cn.kgc.bean.pojo.CUser;
import cn.kgc.common.Constants;
import cn.kgc.common.Result;
import cn.kgc.service.CourtService;
import cn.kgc.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private CourtService courtService;


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody CUser user,
                        @RequestParam(defaultValue = "false")boolean rememberMe){
        //1获取subject对象
        Subject subject = SecurityUtils.getSubject();
        String username = user.getUsername();
        String password = user.getPassword();
        System.out.println(rememberMe);
        //2封装请求数据 到token
        AuthenticationToken token = new UsernamePasswordToken(username, password,true);
        //3调用login方法进行登录认证
        try {
            subject.login(token);
            UserDto udto = userService.getInfo(username);
            return Result.success(Constants.CODE_200,"登录成功",udto);
        }
        catch (UnknownAccountException e) {
            e.printStackTrace();
            return Result.error(Constants.CODE_201,"用户名错误");
        }
        catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            return Result.error(Constants.CODE_201,"密码错误");
        }
        catch (AuthenticationException e) {
            e.printStackTrace();
            System.out.println("登录失败！");
            return Result.error(Constants.CODE_600,"系统错误");
        }
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(required = false) Integer pageNum,
                           @RequestParam(required = false) Integer pageSize,
                           @RequestParam(defaultValue = " ") String username,
                           @RequestParam(defaultValue = " ") String email,
                           @RequestParam(defaultValue = " ") String address) {
        QueryWrapper<CUser> queryWrapper = new QueryWrapper<>();
        if (!" ".equals(username)){
            queryWrapper.like("username",username);
        }
        if (!" ".equals(email)){
            queryWrapper.like("email",email);
        }
        if (!" ".equals(address)){
            queryWrapper.like("address",address);
        }
        queryWrapper.orderByDesc("id");
        //获取当前用户信息
        // User currentUser = TokenUtils.getCurrentUser();
        // System.out.println(currentUser.getNickname());
        Page<CUser> page = userService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
    //登录认证验证RememberMe

    @GetMapping("/username/{username}")
    public Result findUserByUsername(@PathVariable String username){
        CUser userInfo = userService.getUserInfo(username);
        return Result.success(Constants.CODE_200,"",userInfo);
    }

    //登录认证验证角色
    @RequiresRoles("admin")
    @GetMapping("/userLoginRoles")
    public String userLoginRoles(){
        System.out.println("登录认证验证角色");
        return "验证成功";
    }
    //登录认证验证角色
    @RequiresPermissions("user:delete")
    @GetMapping("/userPermissions")
    public String userLoginPermissions(){
        System.out.println("登录认证验证权限");
        return "验证成功";
    }

    /**
     * 登录选择法院
     */
    @PostMapping("/getAllCourt")
    public Result getAllCourt(){
        List<CCourt> cCourt = courtService.getAllCourt();
        return Result.success("200","成功",cCourt);
    }
}
